//
//  Session.m
//  Normal
//
//  Created by 8b410-2 on 16/12/9.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCAccountTools.h"
#import "AppDelegate.h"
#import "OSCNetAPIClient.h"
#define KEY_Name_List @"userNameList" // 名字列表
@implementation OSCAccountTools
- (instancetype)init {
    self = [super init];
    if (self) {
        _account = [self loadAccount];
    }
    return self;
}
+ (instancetype)shareSession {
    static id session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        session = [[OSCAccountTools alloc] init];
    });
    return session;
}

#define BLUserKey @"UserKey"
- (void)saveAccount:(OSCAccount *)account {
    if (nil == account) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:BLUserKey];
    }
    else {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:account];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:BLUserKey];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (OSCAccount *)loadAccount {
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:BLUserKey];
    if (nil == data) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

#pragma mark - 登录相关
#pragma mark 登录成功
- (void)loginSuccess:(OSCAccount *)account {
    // 登录成功
    self.account = account;
    [self saveAccount:_account];
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Login object:nil];
}

- (void)updateAccount:(OSCAccount *)account {
    _account = account;
    [self saveAccount:_account];
}

#pragma mark - 判断是否已经登录
+ (BOOL)isLogin {
    return [[OSCAccountTools shareSession] account] != nil;
}
#pragma mark - 退出登录
- (void)accountLogout; {
    [self saveAccount:nil];
    _account = nil;
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
//登录用户名列表
+ (void) saveLastLoginUserName:(NSString *)userName {
    NSArray *list = [OSCAccountTools getDefaultNameList];
    NSMutableArray *nameList = [list mutableCopy];
    if ([nameList indexOfObject:userName] == NSNotFound) {
        
        [nameList insertObject:userName atIndex:0];
    }else {
        [nameList removeObject:userName];
        [nameList insertObject:userName atIndex:0];
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:nameList forKey:KEY_Name_List];
    [defaults synchronize];
}

+ (NSString *)getLastLoginUserName {
    NSArray *list = [self getDefaultNameList];
    if (list.count==0) {
        return nil;
    }
    return [list objectAtIndex:0];
}


+ (NSArray *) getDefaultNameList {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *nameList = [defaults objectForKey:KEY_Name_List];
    if (nameList == nil) {
        nameList = [[NSArray alloc] init];
    }
    return nameList;
}
#pragma mark - 网络请求
+ (RACSignal *)requestUserWithParamsModel:(OSCBaseRequestParams *)params {
    return [[[OSCNetAPIClient shareJsonClient]
             requestJsonDataWithPath:my_information
             Params:[params mj_keyValues]]
            map:^id(NSDictionary *dict) {
                return [OSCUser mj_objectWithKeyValues:dict];
            }];
}

+ (RACSignal *)requestTokenWithParamsModel:(OSCOAuthRequestParams *)params {
    return [[[OSCNetAPIClient shareJsonClient]
             requestJsonDataWithPath:oauth2_token
             Params:[params mj_keyValues]]
            map:^id(NSDictionary *dict) {
                return [OSCAccount mj_objectWithKeyValues:dict];
            }];
}

+ (RACSignal *)requestValidationAccountWithParamsModel:(OSCValidationAccountRequestParams *)params {
    return [[[OSCNetAPIClient shareJsonClient]
            requestJsonDataWithPath:hash_login Params:[params mj_keyValues]]
            map:^id(NSDictionary *dict) {
                return [OSCValidationAccount mj_objectWithKeyValues:dict];
            }];
}
@end
